package com.htx.zqs.blesmartmask.ui.fragment.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.R;
import com.htx.zqs.blesmartmask.bean.BleDeviceBean;
import com.htx.zqs.blesmartmask.utils.AlertUtils;
import com.htx.zqs.blesmartmask.utils.MySpUtils;
import com.htx.zqs.blesmartmask.utils.Utils;

import java.util.HashMap;

import static com.htx.zqs.blesmartmask.utils.MySpUtils.KEY_STAR_AUTO_INCREMENT;

/**
 * Created by KomoriWu
 * on 2019/2/17.
 */
public class DeviceNewAdapter extends BaseQuickAdapter<BleDeviceBean, BaseViewHolder> {
    private Context mContext;
    private HashMap<String, Integer> mMapNum;

    public DeviceNewAdapter(Context mContext) {
        super(R.layout.item_device);
        this.mContext = mContext;
        mMapNum = new HashMap<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, BleDeviceBean item) {
        int pos = helper.getAdapterPosition() - 1;
        helper.setText(R.id.tv_name, (pos + 1) + "." + mContext.getString(R.string.app_name));

        boolean isRunning = MySpUtils.getBoolean(KEY_STAR_AUTO_INCREMENT, false);
        helper.setGone(R.id.tv_course, isRunning && isConn(item.getBleDevice()));
        if (isRunning && isConn(item.getBleDevice())) {
            helper.setText(R.id.tv_course, "（" + Utils.getCourseNameInternal(mContext,
                    MyApplication.runningCourse.getCoursedate()) + "）");
        }

        String mac = item.getBleDevice().getMac();
        if (!TextUtils.isEmpty(mac)) {
            if (mac.length() > 2) {
                helper.setText(R.id.tv_mac, mac.substring(mac.length() - 5, mac.length()));
            }
        }
        helper.setBackgroundColor(R.id.layout_item, pos % 2 == 0 ? mContext.getResources().getColor(
                R.color.smartmask) : mContext.getResources().getColor(R.color.white));
        helper.setVisible(R.id.iv_conned, isConn(item.getBleDevice()));

        int electric = item.getElectric();
        helper.setVisible(R.id.iv_electric, electric != -1);
        if (!isConn(item.getBleDevice()) || electric == -1) return;
        if (electric >= getIntElectric("c8")) {
            helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric4);
        } else if (electric >= getIntElectric("af")) {
            helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric3);
        } else if (electric >= getIntElectric("96")) {
            helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric2);
        } else if (electric >= getIntElectric("7d")) {
            helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric1);
        } else if (electric >= getIntElectric("65")) {
            helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric0);
            lowElectric(helper, item);
        } else if (electric == getIntElectric("64")) { //旧设备有电
            helper.setVisible(R.id.iv_electric, false);
        }

        if (electric <= getIntElectric("5a")) {//旧设备电量低
            int index = 0;
            if (mMapNum.containsKey(mac)) {
                index = mMapNum.get(mac);
            }
            index += 1;
            mMapNum.put(mac, index);
            if (index >= 3) {
                lowElectric(helper, item);
            }

        } else {
            mMapNum.put(mac, 0);
        }

    }

    private void lowElectric(BaseViewHolder helper, BleDeviceBean item) {
        helper.setBackgroundRes(R.id.iv_electric, R.mipmap.electric0);
        AlertUtils.showAlertDialog(mContext, R.string.kindly_reminder_,
                mContext.getString(R.string.power_low_change_device));
        BleManager.getInstance().disconnect(item.getBleDevice());
    }

    private int getIntElectric(String electric) {
        return Integer.parseInt(electric, 16);
    }

    private boolean isConn(BleDevice bean) {
        return BleManager.getInstance().isConnected(bean);
    }
}
